#include "LED.h"
#include "KEY.h"
#include "UserApp.h"
#include "OS_Timer.h"

// xdata uint8   RLedBuf[3],RLedFlashBuf[3];
// Typedef_LED LedColor;
uint8 LedScanLine;
XDATA	U_LED    DispBuf;

void    initLED(void)
{
    uint8 i;

    SET_OUT(H1,LOW);
    SET_OUT(H2,LOW);
    SET_OUT(H3,LOW);
    SET_OUT(V1,HIGH);
    SET_OUT(V2,HIGH);
    SET_OUT(V3,HIGH);

    for(i=0; i<3; i++)
    {
        DispBuf.Buf[i] = 0;
    }
    LedScanLine = 0;
    // memset(&LedColor,0,sizeof(Typedef_LED));
}

void    LED_AllOff(void)
{
    DispBuf.Buf[0] = 0;
    DispBuf.Buf[1] = 0;
    DispBuf.Buf[2] = 0;    
}
void    LED_AllOn(void)
{
    DispBuf.Buf[0] = 0x0F;
    DispBuf.Buf[1] = 0x0F;
    DispBuf.Buf[2] = 0x0F;    
}

// void    LED_Set(uint8 led1, uint8 led2, uint8 led3)
// {
//     // LedColor.LED1 = led1;
//     // LedColor.LED2 = led2;
//     // LedColor.LED3 = led3;

// }

// void    FCB_TouchKey1Blink(void)
// {
// }
// void    FCB_TouchKey2Blink(void)
// {
// }
// void    FCB_TouchKey3Blink(void)
// {
// }
// void    FCB_TouchKey4Blink(void)
// {
// }
// void    FCB_TouchKey5Blink(void)
// {
// }

bit f_Blink_TouchKey1 = NO;
bit f_Blink_TouchKey2 = NO;
bit f_Blink_TouchKey3 = NO;
bit f_Blink_TouchKey4 = NO;
bit f_Blink_TouchKey5 = NO;
bit f_Blink_TouchKey6 = NO;

void    FCB_TouchKeyBlink(void)
{
    OS_StopTimer(TIMER_LED);
    if(f_Blink_TouchKey1)
    {
        f_Blink_TouchKey1 = NO;
        DispBuf.Detail.LED4 = YES;
    }
    if(f_Blink_TouchKey2)
    {
        f_Blink_TouchKey2 = NO;
        DispBuf.Detail.LED5 = YES;
    }
    if(f_Blink_TouchKey3)
    {
        f_Blink_TouchKey3 = NO;
        DispBuf.Detail.LED6 = YES;
    }
    if(f_Blink_TouchKey4)
    {
        f_Blink_TouchKey4 = NO;
        DispBuf.Detail.LED10 = YES;
    }
    if(f_Blink_TouchKey5)
    {
        f_Blink_TouchKey5 = NO;
        DispBuf.Detail.LED11 = YES;
    }
    if(f_Blink_TouchKey6)
    {
        f_Blink_TouchKey6 = NO;
        DispBuf.Detail.LED12 = YES;
    }
}

#if 0
void    LED_DispTouchKey(u8 key)
{
    if(key & 0x01)    
    {
        if(DispBuf.Detail.LED4 == NO)
            DispBuf.Detail.LED4 = YES;
        else
        {
            DispBuf.Detail.LED4 = NO;
            f_Blink_TouchKey1 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    }
    if(key & 0x02)
    {
        if(DispBuf.Detail.LED5 == NO)
            DispBuf.Detail.LED5 = YES;
        else
        {
            DispBuf.Detail.LED5 = NO;
            f_Blink_TouchKey2 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    }    
    if(key & 0x04)
    {
        if(DispBuf.Detail.LED6 == NO)
            DispBuf.Detail.LED6 = YES;
        else
        {
            DispBuf.Detail.LED6 = NO;
            f_Blink_TouchKey3 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    }    
    if(key & 0x08)
    {
        if(DispBuf.Detail.LED10 == NO)
            DispBuf.Detail.LED10 = YES;
        else
        {
            DispBuf.Detail.LED10 = NO;
            f_Blink_TouchKey4 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    } 
    if(key & 0x10)
    {
        if(DispBuf.Detail.LED11 == NO)    
            DispBuf.Detail.LED11 = YES;
        else
        {
            DispBuf.Detail.LED11 = NO;
            f_Blink_TouchKey5 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    }    
    if(key & 0x20)
    {
        if(DispBuf.Detail.LED12 == NO)    
            DispBuf.Detail.LED12 = YES;
        else
        {
            DispBuf.Detail.LED12 = NO;
            f_Blink_TouchKey6 = YES;
            OS_StartTimer(TIMER_LED,TIMER_200MS,FCB_TouchKeyBlink);
        }
    }

}
void    LED_ClrDispTouchKey(void)
{
    DispBuf.Detail.LED4 = NO;
    DispBuf.Detail.LED5 = NO;
    DispBuf.Detail.LED6 = NO;
    DispBuf.Detail.LED10 = NO;
    DispBuf.Detail.LED11 = NO;
    DispBuf.Detail.LED12 = NO;
}

#endif


code uint8 TAB_DISP_LED_PMD[] = {0x00,0x01,0x03,0x07,0x0F,0x1F};
void    LED_DisplayPaoMoDun(u8 level)
{
    DispBuf.Buf[0] = TAB_DISP_LED_PMD[level];
}


code uint8 TAB_DISP_LED[] = {0x00,0x01,0x21,0x03,0x43,0x07};
void    LED_DisplayLevel(u8 level)
{
    DispBuf.Buf[0] = TAB_DISP_LED[level];
    // if(level == 3)
    // {
    //     DispBuf.Detail.LED1 = YES;
    //     DispBuf.Detail.LED2 = YES;
    //     DispBuf.Detail.LED3 = YES;
    // }
    // else if(level == 2)
    // {
    //     DispBuf.Detail.LED1 = YES;
    //     DispBuf.Detail.LED2 = YES;
    //     DispBuf.Detail.LED3 = NO;
    // }
    // else if(level == 1)
    // {
    //     DispBuf.Detail.LED1 = YES;
    //     DispBuf.Detail.LED2 = NO;
    //     DispBuf.Detail.LED3 = NO;
    // }
    // else
    // {
    //     DispBuf.Detail.LED1 = NO;
    //     DispBuf.Detail.LED2 = NO;
    //     DispBuf.Detail.LED3 = NO;
    // }
}

vuint8 CntTimeKeyLed = 0;
// vuint8 KeyLed;

#if 0
void LED_DisplayKey(void)
{
    DispBuf.Detail.LED4 = YES;
    DispBuf.Detail.LED5 = YES;
    DispBuf.Detail.LED6 = YES;
    DispBuf.Detail.LED10 = YES;
    DispBuf.Detail.LED11 = YES;
    DispBuf.Detail.LED12 = YES;
}
#else
void LED_DisplayKey(uint8 key)
{
    DispBuf.Detail.LED4 = NO;
    DispBuf.Detail.LED5 = NO;
    DispBuf.Detail.LED6 = NO;
    DispBuf.Detail.LED10 = NO;
    DispBuf.Detail.LED11 = NO;
    DispBuf.Detail.LED12 = NO;

    switch(key)
    {
        case LED_KEY1:
            DispBuf.Detail.LED4 = YES;
        break;

        case LED_KEY2:
            DispBuf.Detail.LED5 = YES;
        break;

        case LED_KEY3:
            DispBuf.Detail.LED6 = YES;
        break;

        case LED_KEY4:
            DispBuf.Detail.LED10 = YES;
        break;

        case LED_KEY5:
            DispBuf.Detail.LED11 = YES;
        break;

        case LED_KEY6:
            DispBuf.Detail.LED12 = YES;
        break;

        case LED_KEY_OFF:
        default:
        break;      
    }
}

#endif


void    LED_Main(void)
{
    uint8 disp_buf;

    SET_OUT(H1,LOW);
    SET_OUT(H2,LOW);
    SET_OUT(H3,LOW);

    disp_buf = DispBuf.Buf[LedScanLine];
    if(disp_buf & 0x10)
    {
        if(f_Flash)            SET_OUT(V1,LOW);
        else                   SET_OUT(V1,HIGH);
    }
    else
    {
        if(disp_buf & 0x01)    SET_OUT(V1,LOW);
        else                   SET_OUT(V1,HIGH);
    }
    
    if(disp_buf & 0x20)
    {
        if(f_Flash)            SET_OUT(V2,LOW);
        else                   SET_OUT(V2,HIGH);
    }
    else
    {
        if(disp_buf & 0x02)    SET_OUT(V2,LOW);
        else                   SET_OUT(V2,HIGH);
    }
    
    if(disp_buf & 0x40)
    {
        if(f_Flash)            SET_OUT(V3,LOW);
        else                   SET_OUT(V3,HIGH);
    }
    else
    {
        if(disp_buf & 0x04)    SET_OUT(V3,LOW);
        else                   SET_OUT(V3,HIGH);
    }

    switch (LedScanLine)
    {
    case 0:
    SET_OUT(H1,HIGH);
    LedScanLine++;
    break;
    
    case 1:
    SET_OUT(H2,HIGH);
    LedScanLine++;
    break;

    case 2:
    SET_OUT(H3,HIGH);
    LedScanLine = 0;
    break;

    default:
        LedScanLine = 0;
        break;
    }
}

void    LED_PowerSaveSetting(void)
{
    SET_OUT(H1,LOW);
    SET_OUT(H2,LOW);
    SET_OUT(H3,LOW);
    LED_AllOff();
    memset(&DispFlg,0,sizeof(Typedef_DISP_FLAG));
}